import type { FormItemInstance } from './types'
import { inputProps } from './attr'

// 创建新的输入框实例
export function createInputInstance(): FormItemInstance {
    return {
        id: `input_${Date.now()}`,
        type: 'input',
        props: {
            base: Object.fromEntries(
                Object.entries(inputProps.base).map(([key, prop]) => [key, prop.default])
            ),
            style: Object.fromEntries(
                Object.entries(inputProps.style).map(([key, prop]) => [key, prop.default])
            ),
            rules: Object.fromEntries(
                Object.entries(inputProps.rules).map(([key, prop]) => [key, prop.default])
            ),
        },
    }
}

// 验证输入值
export function validateInput(value: string, rules: FormItemInstance['props']['rules']): boolean {
    if (rules.required && !value) {
        return false
    }
    if (rules.maxLength && value.length > rules.maxLength) {
        return false
    }
    if (rules.pattern && !new RegExp(rules.pattern).test(value)) {
        return false
    }
    return true
} 